<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category LoginController.php
 * @Depiction:
 * @Author: hwy
 * @Date: 2021/4/5  17:31
 * +--------------------------------------------------------+
 */

namespace App\Http\Controllers\Index;


use App\Http\Controllers\Controller;
use App\Models\User;
use App\Services\SocialiteService;
use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;
use Auth;

class LoginController extends Controller
{
    private $socialiteService;

    public function __construct()
    {
        $this->socialiteService = SocialiteService::getInstance();
    }

    public function qq()
    {
        if (env('APP_ENV') == 'local') {
            $user = User::query()->first();
            Auth::login($user, true);
            return redirect('/article/index');
        }

        return Socialite::with('qq')->redirect();
    }

    //账号登录-暂无
    public function accountLogin(Request $request)
    {
        if (Auth::attempt(['name' => $request->username, 'password' => $request->password])) {
            return redirect('/article/index');
        } else {
            return redirect('/article/index');
        }
    }

    public function qqlogin()
    {
        $user = Socialite::driver('qq')->stateless()->user();
//        dd($user);
        $this->socialiteService->QqLogin($user);

        return redirect('/article/index');
    }


    public function logout(Request $request)
    {
        Auth::guard()->logout();
        $request->session()->invalidate();
        $request->session()->regenerateToken();
        return redirect('/');
    }

}
